#Load required libraries
library(ggplot2)
library(dplyr)
library(reshape2)
library(tidyr)
library(janitor)
library(stringr)
library(ggthemes)

#import data
#get filenames for all  .csv results
RGoUfilenames <- list.files(path="~/Desktop/Research/RGoU/RGoU Metrics", pattern ="*.csv")
TwSqfilenames<-list.files(path="~/Desktop/Research/RGoU/20 Squares Metrics", pattern ="*.csv")
#Create list of data frame names without the ".csv" part 
RGoUnames <-tools::file_path_sans_ext(RGoUfilenames)   
TwSqnames <-tools::file_path_sans_ext(TwSqfilenames)  

#Load all files
for(i in RGoUnames){
  RGoUfilepath <- file.path("~/Desktop/Research/RGoU/RGoU Metrics",paste(i,".csv",sep=""))
  assign(i, read.delim(RGoUfilepath,header = FALSE,
                       sep = ","))
}

for(i in TwSqnames){
  TwSqfilepath <- file.path("~/Desktop/Research/RGoU/20 Squares Metrics",paste(i,".csv",sep=""))
  assign(i, read.delim(TwSqfilepath,header = FALSE,
                       sep = ","))
}
#Build Royal Game of Ur dataframe
#import filenames
RGoUAll<-as.list(RGoUnames)
#create dataframe from list
RGoUdf <- do.call(rbind, RGoUAll)[, 1]
RGoUDB<-do.call(rbind, mget(RGoUdf))
RGoUDB <- tibble::rownames_to_column(RGoUDB, "Ruleset")
RGoUDB$Ruleset<-str_sub(RGoUDB$Ruleset, end = -3) 
RGoUDBT<-dcast(RGoUDB, Ruleset~V1)
#add column identiying the game
RGoUDBT$Game<-"Royal Game of Ur"

#Create Twenty squares dataframe
TwSqAll<-as.list(TwSqnames)
TwSqdf <- do.call(rbind, TwSqAll)[, 1]
TwSqDB<-do.call(rbind, mget(TwSqdf))
TwSqDB <- tibble::rownames_to_column(TwSqDB, "Ruleset")
TwSqDB$Ruleset<-str_sub(TwSqDB$Ruleset, end = -3) 
TwSqRes<-data.frame(t(TwSqDB))
TwSqDBT<-dcast(TwSqDB, Ruleset~V1)
TwSqDBT$Game<-"Twenty Squares"

#Add column calculating the difference in drama between RGoU and Twenty squares
RGoUDBT$DramaDiff<-cbind(TwSqDBT$`TimesSwitchAdvantageAvg=`-RGoUDBT$`TimesSwitchAdvantageAvg=`)
#Add column calculating the difference in advantage between RGoU and Twenty squares
RGoUDBT$P1Diff<-cbind(TwSqDBT$`AdvantageP1=`-RGoUDBT$`AdvantageP1=`)
#Add column calculating the difference in duration between RGoU and Twenty squares
RGoUDBT$DurDiff<-cbind(TwSqDBT$`DurationAvg=`-RGoUDBT$`DurationAvg=`)
#Add column calculating the % difference in duration between RGoU and Twenty squares
RGoUDBT$DurPercent<-cbind(RGoUDBT$`DurDiff`/RGoUDBT$`DurationAvg=`)
#Add column calculating the %difference in drama between RGoU and Twenty squares
RGoUDBT$DramaPercent<-cbind(RGoUDBT$`DramaDiff`/RGoUDBT$`TimesSwitchAdvantageAvg=`)
#Add column calculating the % difference in advantage between RGoU and Twenty squares
RGoUDBT$P1Percent<-cbind(RGoUDBT$`P1Diff`/RGoUDBT$`AdvantageP1=`)
RGoUDBT$`P1DiffAbs` <- abs(RGoUDBT$`P1Diff`)
#add columns for the dice and rosettes
RGoUDBT$Pieces <- ifelse(grepl("5", RGoUDBT$Ruleset, ignore.case = T), "Five", "Seven")
RGoUDBT$Dice<-c("Astragal", "D6", "Seleukid", "Tetrahedra", "Astragal", "D6", "Seleukid", "Tetrahedra","Astragal", "D6", "Seleukid", "Tetrahedra","Astragal", "D6", "Seleukid", "Tetrahedra","Astragal", "D6", "Seleukid", "Tetrahedra","Astragal", "D6", "Seleukid", "Tetrahedra","Astragal", "D6", "Seleukid", "Tetrahedra","Astragal", "D6", "Seleukid", "Tetrahedra", "Astragal", "D6", "Seleukid", "Tetrahedra","Astragal", "D6", "Seleukid", "Tetrahedra")
RGoUDBT$Rosette<-c("Finkel","Finkel","Finkel","Finkel","Finkel","Finkel","Finkel","Finkel","Free and Safe","Free and Safe","Free and Safe","Free and Safe","Free and Safe","Free and Safe","Free and Safe","Free and Safe", "Free turn","Free turn","Free turn","Free turn","Free turn","Free turn","Free turn","Free turn","No captures", "No captures","No captures","No captures","No captures","No captures","No captures","No captures","No captures free turn","No captures free turn","No captures free turn","No captures free turn","No captures free turn","No captures free turn","No captures free turn","No captures free turn")

#repeat in twenty squares dataframe
TwSqDBT$DramaDiff<-cbind(TwSqDBT$`TimesSwitchAdvantageAvg=`-RGoUDBT$`TimesSwitchAdvantageAvg=`)
TwSqDBT$P1Diff<-cbind(TwSqDBT$`AdvantageP1=`-RGoUDBT$`AdvantageP1=`)
TwSqDBT$DurDiff<-cbind(TwSqDBT$`DurationAvg=`-RGoUDBT$`DurationAvg=`)
TwSqDBT$DurPercent<-cbind(RGoUDBT$`DurDiff`/RGoUDBT$`DurationAvg=`)
TwSqDBT$DramaPercent<-cbind(RGoUDBT$`DramaDiff`/RGoUDBT$`TimesSwitchAdvantageAvg=`)
TwSqDBT$P1Percent<-cbind(RGoUDBT$`P1Diff`/RGoUDBT$`AdvantageP1=`)
TwSqDBT$`P1DiffAbs` <- abs(RGoUDBT$`P1Diff`)
TwSqDBT$Dice<-c("Astragal", "D6", "Seleukid", "Tetrahedra", "Astragal", "D6", "Seleukid", "Tetrahedra","Astragal", "D6", "Seleukid", "Tetrahedra","Astragal", "D6", "Seleukid", "Tetrahedra","Astragal", "D6", "Seleukid", "Tetrahedra","Astragal", "D6", "Seleukid", "Tetrahedra","Astragal", "D6", "Seleukid", "Tetrahedra","Astragal", "D6", "Seleukid", "Tetrahedra", "Astragal", "D6", "Seleukid", "Tetrahedra","Astragal", "D6", "Seleukid", "Tetrahedra")
TwSqDBT$Rosette<-c("Finkel","Finkel","Finkel","Finkel","Finkel","Finkel","Finkel","Finkel","Free and Safe","Free and Safe","Free and Safe","Free and Safe","Free and Safe","Free and Safe","Free and Safe","Free and Safe", "Free turn","Free turn","Free turn","Free turn","Free turn","Free turn","Free turn","Free turn","No captures", "No captures","No captures","No captures","No captures","No captures","No captures","No captures","No captures free turn","No captures free turn","No captures free turn","No captures free turn","No captures free turn","No captures free turn","No captures free turn","No captures free turn")
TwSqDBT$Pieces <- ifelse(grepl("5", TwSqDBT$Ruleset, ignore.case = T), "Five", "Seven")

#Combine RGoU and Twenty Squares dataframes
Metrics<-rbind(RGoUDBT, TwSqDBT)
Metrics$TimesSwitchAdvantageAvg<-as.numeric(Metrics$TimesSwitchAdvantageAvg)
Metrics$Ruleset<-as.character(Metrics$Ruleset)
Metrics$Ruleset <- str_remove(Metrics$Ruleset, pattern = "RGoU")
Metrics$Ruleset <- str_remove(Metrics$Ruleset, pattern = "TS")
#create column for dice 
Metrics$GameDice<-paste(Metrics$Game, Metrics$Dice)

#set color palette
scale_color_colorblind7 = function(.ColorList = 2L:8L, ...){
  scale_color_discrete(..., type = colorblind_pal()(8)[.ColorList])
}

#visualization
DramaPlot<-ggplot(Metrics, aes(x=`TimesSwitchAdvantageAvg=`,y=reorder(Ruleset,`DramaDiff`), color=Rosette, size=Pieces, shape=GameDice))+geom_point()+theme(axis.text.x=element_text(angle = 45, hjust=1))+scale_shape_manual(values=c(1,0,5,2,16,15,18,17))+  scale_color_colorblind7(.ColorList = c(3,4,7,6,1))

DurPlot<-ggplot(Metrics, aes(x=`DurationAvg=`,y=reorder(Ruleset,`DurDiff`), color=Rosette, size=Pieces, shape=GameDice))+geom_point()+theme(axis.text.x=element_text(angle = 45, hjust=1))+scale_shape_manual(values=c(1,0,5,2,16,15,18,17))+  scale_color_colorblind7(.ColorList = c(3,4,7,6,1))

P1DiffPlot<-ggplot(Metrics, aes(x=`AdvantageP1=`,y=reorder(Ruleset,-`P1Diff`), color=Rosette, size=Pieces, shape=GameDice))+geom_point()+theme(axis.text.x=element_text(angle = 45, hjust=1))+scale_shape_manual(values=c(1,0,5,2,16,15,18,17))+  scale_color_colorblind7(.ColorList = c(3,4,7,6,1))
P1DiffPlot







